/**
 * 升级包表单页面
 * wangmei
 */
import React, { Component } from 'react';
import { Modal, Button, Form, Input, message, Select, Radio, Row, Col } from 'antd';
import * as Request from '../../../Server/Upgrade';
import * as Const from '../../../common/const';
import qs from 'qs';

const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;

class PatchFrom extends Component{
    constructor(props) {
        super(props);
        this.state = {
            langData: []
        }
    }

    componentWillMount(){
        this.getLangData();
    }

    //保存方法
    handleSubmit = (e) => {
        e.preventDefault();
        const { form, row, id, current, size } = this.props
        form.validateFieldsAndScroll((err, values) => {
            if(!err){
                values.versionId = id;
                values.path = values.path.replace(/\s*/g,"");
                values.md5 = values.md5.replace(/\s*/g,"");
                values.index = values.index.replace(/\s*/g,"");
                if(row.id==undefined){ //新增
                    const _param = qs.stringify(values);
                    Request.patchInfoSave(_param).then(res => {
                        if(res && res.code == Const.REQUEST_SUCCESS){
                            this.props.showModelFunc({} , false);
                            this.props.getTableData({page: current?current-1:0, size: size(), vId: id});
                            message.success('新增成功！', 1);
                        }else{
                            res.code !== Const.REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                        }
                    });
                }else {
                    values.id = row.id;
                    const _param = qs.stringify(values);
                    Request.patchInfoUpdate(_param).then(res => {
                        if(res && res.code == Const.REQUEST_SUCCESS){
                            this.props.showModelFunc({} , false);
                            this.props.getTableData({page: current?current-1:0, size: size(), vId: id});
                            message.success('修改成功！', 1);
                        }else{
                            res.code !== Const.REQUEST_UNAUTHORIZED && message.error(res.msg, 2);
                        }
                    });
                }
            }
        });
    }

    //语言ID
    getLangData = (name) =>{
        const param = {
            page: 0, 
            size: 50 
        }
        const _param = qs.stringify(param);
        Request.languagesPage(_param).then(res => {
            if(res && res.code == Const.REQUEST_SUCCESS){
                const data = res.data
                this.setState({
                    langData: data.content,
                });
            }else{
                res.code !== Const.REQUEST_UNAUTHORIZED && message.error(res.msg);
            }
        });
    }

    validateSize= (rule, value, callback) => {
        const reg = /^[0-9]*$/;
        if(!reg.test(value)){
            callback('请输入数值类型的数据！');
            return;
        }
        if(value.length>9){
            callback('包的大小长度不能超过9个字符');
            return;
        }
        if (value == '' || value == undefined) {
            callback('请输入包的大小！');
            return;
        }
        callback();
    }

    render(){
        const { showModelFunc, row } = this.props;
        const { getFieldDecorator } = this.props.form;
        const { langData } = this.state;
        const langOptions = langData.map(d => <Option value={d.id}>{d.languageNo}</Option>);
        const formItemLayout = {
            labelCol: {
              xs: { span: 24 },
              sm: { span: 8 },
            },
            wrapperCol: {
              xs: { span: 24 },
              sm: { span: 12 },
            },
        };

        //类型数据
        const typeData = [
            {
                title: 'selfpatch',
                value: 'selfpatch',
            }, {
                title: 'bigpatch',
                value: 'bigpatch',
            }, {
                title: 'diffpatch',
                value: 'diffpatch',
            }, {
                title: 'busy',
                value: 'busy',
            }, {
                title: 'normalpatch',
                value: 'normalpatch',
            }, {
                title: 'noconditionpatch',
                value: 'noconditionpatch',
            }
        ];

        return(
            <Modal 
                visible={true}
                width="50%"
                title={row.id?"修改升级包信息":"新增升级包信息"}
                footer={null}
                maskClosable={false}
                onCancel={()=>showModelFunc({} , false)}
            >
                <Form onSubmit={this.handleSubmit} >
                    <FormItem
                        {...formItemLayout}
                        label="升级包类型"
                        >
                        {getFieldDecorator('type',{
                            rules: [{
                                required: true, 
                                message: '请选择升级包类型！'
                            }],
                            initialValue: row.type || 'selfpatch',
                        })(
                            <Select style={{ width: '100%'}} >
                                {
                                    typeData.map((item, index)=>{
                                        return <Option value={item.value} >
                                                {item.title}
                                            </Option>
                                    })
                                }
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="索引"
                        >
                        {getFieldDecorator('index', {
                            rules: [{
                                required: true, 
                                message: '请输入索引信息！'
                            }],
                            initialValue: row.index || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="包的位置"
                        >
                        {getFieldDecorator('path', {
                            rules: [{
                                required: true, 
                                message: '请输入包的位置！'
                            }],
                            initialValue: row.path || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="md5"
                        >
                        {getFieldDecorator('md5', {
                            rules: [{
                                required: true, 
                                message: '请输入md5信息！'
                            }],
                            initialValue: row.md5 || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="包的大小"
                        >
                        {getFieldDecorator('size', {
                            rules: [{
                                required: true, 
                                validator: this.validateSize
                            }],
                            initialValue: row.size || '',
                        })(
                            <Input maxLength={255} style={{ width: '100%'}} />
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="本地目录"
                        >
                        {getFieldDecorator('localdir', {
                            initialValue: row.localdir || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="执行时参数"
                        >
                        {getFieldDecorator('args', {
                            initialValue: row.args || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="需要基础版本"
                        >
                        {getFieldDecorator('baseversion', {
                            initialValue: row.baseversion || '',
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="语言编号"
                        >
                        {getFieldDecorator('langId', {
                            rules: [{
                                required: true, 
                                message: '请选择语言编号！'
                            }],
                            initialValue: row.langId?parseInt(row.langId):16,
                        })(
                            <Select
                                showSearch
                                style={{ width: '100%' }}
                                optionFilterProp="children"
                                onSelect={this.getLangData}
                                filterOption={true}
                            >
                                {langOptions}
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="备注"
                        >
                        {getFieldDecorator('remark', {
                            initialValue: row.remark || '',
                        })(
                            <Input maxLength={255} type="" placeholder="备注，250个字符以内"/>
                        )}
                    </FormItem>
                    <Row type="flex" style={{ textAlign: 'center'}}>
                        <Col span={24}>
                            <FormItem>
                                <Button type="primary" htmlType="submit"  className="conform-button">保存</Button>
                                <Button type="primary" onClick={()=>showModelFunc({} , false)}>返回</Button>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>

            </Modal>
        )
    }
}

export default Form.create()(PatchFrom)